<?php
namespace App\Api\Controllers\Zgjl;


use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class BaseController
{
    static $config, $instance, $data, $sign;

    const MAIN_URL ='http://test.hccb.cc:9090/ifsp-payweb/SOA/txnAccept';

    const QR_URL = 'https://cloud.com-link.cn/comlinkpay/api/bocpay/transfer';

    const SCAN_PAY_URL = 'https://cloud.com-link.cn/bocfpay/comlink/pay';

    public static function getInstance ()
    {
        if (! isset(self::$instance)) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    public function params ($body)
    {
        $key = $body['key'];
        unset($body['key']);
        self::$data = $body;
        self::$sign = $this->sign($body, $key);
        return $this;
    }

    /**
     * 加签
     * @param string $data
     */
    private function sign($data, $key)
    {
        return md5(json_encode($data).'&'.$key);
    }


    /**
     * ASCII排序
     * @param array $params
     * @param $key
     * @return bool|string
     */
    public function getSignContent ($params)
    {
        ksort($params);

        $stringToBeSigned = "";
        $i = 0;
        foreach ($params as $k => $v) {
            if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {

                // 转换成目标字符集
                $v = $this->charCet($v, 'UTF-8');

                if ($i == 0) {
                    $stringToBeSigned .= "$k" . "=" . "$v";
                } else {
                    $stringToBeSigned .= "&" . "$k" . "=" . "$v";
                }
                $i++;
            }
        }

        unset ($k, $v);
        return $stringToBeSigned;
    }

    public function verify ($data, $header)
    {

        if (isset($data['merTradeNo']) && !empty($data['merTradeNo'])) {
            $merchant = DB::table("zgjl_stores")->where('mch_id', $data['oldTermId'])->first();
            $sign = md5(json_encode($data).'&'.$merchant->key);
            if ($sign == $header['sign'] && $data['respCode'] == '00') {
                return true;
            }
        }

        if (isset($data['merTradeNo']) && !empty($data['merTradeNo'])) {
            $merchant = DB::table("zgjl_stores")->where('mch_id', $data['oldTermId'])->first();
            $payData = [
                "mch_id" => $merchant->mch_id,
                "pos_no" => $merchant->pos_no,
                "key" => $merchant->key,
                "out_trade_no" =>$data['merTradeNo']
            ];

            $return = (new PayController())->order_query($payData);
            if ($return['status'] == 1) {
                return true;
            }
        }
        return false;
    }




    public function build ($fun, $url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url); //设置选项，包括URL
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //绕过ssl验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(self::$data));

        $sign = self::$sign;
        $headers = array(
            "Content-Type: application/json",
            "sign: {$sign}"
        );

        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        $output = curl_exec($ch); //执行并获取HTML文档内容

        curl_close($ch); //释放curl句柄
        Log::channel("zgjl")->info($fun."_Request", [$url, $headers, self::$data]);
        $output = json_decode($output, true);
        Log::channel("zgjl")->info($fun."_Response", $output);
        return $output;

    }

}